package com.whmnx.base.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.whmnx.base.R;

import me.goldze.mvvmhabit.utils.ToastUtils;


/**
 * @author Administrator
 */
public class CommonDialog3 extends Dialog implements View.OnClickListener{
    private TextView contentTxt;
    private TextView submitTxt;
    private TextView cancelTxt;
    private EditText et1,et2;

    private Context mContext;
    private String content;
    private int sss;
    private OnCloseListener listener;
    private OnCloseListener2 listener2;
    private String positiveName;
    private String negativeName;
    private String title,etInput,etInput2;

    public CommonDialog3(Context context) {
        super(context);
        this.mContext = context;
    }
    public CommonDialog3(Context context, String content) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.content = content;
    }

    public CommonDialog3(Context context, int themeResId, String content, int s, OnCloseListener2 listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener2 = listener;
        this.sss = s;
    }

    public CommonDialog3(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
        setCanceledOnTouchOutside(true);
    }

    protected CommonDialog3(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public CommonDialog3 setTitle(String title){
        this.title = title;
        return this;
    }

    public CommonDialog3 setPositiveButton(String name){
        this.positiveName = name;
        return this;
    }

    public CommonDialog3 setNegativeButton(String name){
        this.negativeName = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_commom3);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView(){
        contentTxt = findViewById(R.id.content);
        submitTxt = findViewById(R.id.submit);
        et1 = findViewById(R.id.et1);
        et2 = findViewById(R.id.et2);
        submitTxt.setOnClickListener(this);
        cancelTxt = findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);

        if (sss == 1) {
            et2.setVisibility(View.GONE);
            submitTxt.setText("确认");
        }else if (sss == 2){
            et1.setHint("请输入商品规格");
            et2.setHint("请输入商品价格");
            et2.setVisibility(View.VISIBLE);
        }else if (sss == 3){
            et2.setVisibility(View.VISIBLE);
            et1.setVisibility(View.VISIBLE);
        }else if (sss == 4){
              et2.setVisibility(View.VISIBLE);
              et1.setVisibility(View.GONE);
        }


        contentTxt.setText(content);
        if(!TextUtils.isEmpty(positiveName)){
            submitTxt.setText(positiveName);
        }

        if(!TextUtils.isEmpty(negativeName)){
            cancelTxt.setText(negativeName);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.cancel) {
            if (listener != null) {
                listener.onClick(this, false);
            }
            this.dismiss();
        } else if (id == R.id.submit) {
            if (sss == 1){
                if (listener2 != null) {
                    etInput = et1.getText().toString().trim();
                    if (TextUtils.isEmpty(etInput)){
                        ToastUtils.showShort("请输入");
                        return;
                    }
                    listener2.onClick(this, true,etInput,"");
                }
            }else if (sss == 2 || sss == 3){
                if (listener2 != null) {
                    etInput = et1.getText().toString().trim();
                    etInput2 = et2.getText().toString().trim();
                    if (TextUtils.isEmpty(etInput)){
                        ToastUtils.showShort(et1.getHint().toString());
                        return;
                    }
                    if (TextUtils.isEmpty(etInput2)){
                        ToastUtils.showShort(et2.getHint().toString());
                        return;
                    }
                    listener2.onClick(this, true,etInput,etInput2);
                }
            }else {
                if (listener != null) {
                    listener.onClick(this, true);
                }
            }
        }
    }

    public interface OnCloseListener{
        void onClick(Dialog dialog, boolean confirm);
    }

    public interface OnCloseListener2{
        void onClick(Dialog dialog, boolean confirm, String s, String s2);
    }
}
